/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConsultaEnderecoDialog.java
 *
 * Created on 24/10/2011, 16:16:24
 */
package br.com.seguradora.view.dialog;

import br.com.seguradora.exception.PersistenciaException;
import br.com.seguradora.model.Sinistro;
import br.com.seguradora.service.SinistroService;
import br.com.seguradora.view.StateEntityWindow;
import com.towel.swing.table.ObjectTableModel;
import com.towel.swing.table.Resizer;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.KeyEvent;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author Diego
 */
public class ConsultaSinistroDialog extends javax.swing.JDialog {

    private ObjectTableModel<Sinistro> tableModel;
    private final static String titleWindow = "Controle de Sinistros";
    private final String[] filters = {"Associado", "Placa"};
    private StateEntityWindow stateEntity;
    private static final String PLACA_MASK = "UUU-####";
    private MaskFormatter maskFormatter;
    private boolean selectEntity;
    private Sinistro sinistro;

    public ConsultaSinistroDialog(java.awt.Frame parent, boolean modal, StateEntityWindow stateEntity) {
        super(parent, modal);
        initComponents();
        this.stateEntity = stateEntity;
        setTitle(titleWindow + stateEntity);
        setLocationRelativeTo(null);
        organizeWindow();
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        filtroCombo = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        pesquisarButton = new javax.swing.JButton();
        termoField = new javax.swing.JFormattedTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        countLabel = new javax.swing.JLabel();
        excluirLink = new org.jdesktop.swingx.JXHyperlink();
        recentesLink = new org.jdesktop.swingx.JXHyperlink();
        adicionarButton = new javax.swing.JButton();
        imprimirButton = new javax.swing.JButton();
        sairButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Opções de pesquisa"));

        jLabel1.setText("Pesquisar por:");

        filtroCombo.setModel(new DefaultComboBoxModel(filters));
        filtroCombo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                filtroComboItemStateChanged(evt);
            }
        });

        jLabel2.setText("Termo da pesquisa:");

        pesquisarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/magnifier.png"))); // NOI18N
        pesquisarButton.setMnemonic('p');
        pesquisarButton.setText("Pesquisar");
        pesquisarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pesquisarButtonActionPerformed(evt);
            }
        });

        termoField.setFocusLostBehavior(javax.swing.JFormattedTextField.PERSIST);
        termoField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                termoFieldActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(filtroCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(pesquisarButton)
                        .addComponent(termoField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 518, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(120, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(filtroCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(termoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pesquisarButton)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Resultado da pesquisa"));

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        table.setShowHorizontalLines(false);
        table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(table);

        countLabel.setText("Nenhum registro localizado");

        excluirLink.setForeground(new java.awt.Color(255, 51, 0));
        excluirLink.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/delete.png"))); // NOI18N
        excluirLink.setText("excluir");
        excluirLink.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                excluirLinkActionPerformed(evt);
            }
        });

        recentesLink.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/information.png"))); // NOI18N
        recentesLink.setMnemonic('v');
        recentesLink.setText("visualizar registros recentes");
        recentesLink.setToolTipText("Visualizar sinistros adicionados recentemente");
        recentesLink.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                recentesLinkActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(countLabel))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(541, 541, 541)
                .addComponent(recentesLink, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(excluirLink, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 774, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(excluirLink, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(recentesLink, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 390, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(countLabel))
        );

        adicionarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/add.png"))); // NOI18N
        adicionarButton.setMnemonic('a');
        adicionarButton.setText("Adicionar sinistro");
        adicionarButton.setToolTipText("Adicionar novo sinistro");
        adicionarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                adicionarButtonActionPerformed(evt);
            }
        });

        imprimirButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/printer.png"))); // NOI18N
        imprimirButton.setMnemonic('i');
        imprimirButton.setText("Imprimir");

        sairButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/cross.png"))); // NOI18N
        sairButton.setMnemonic('s');
        sairButton.setText("Sair");
        sairButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sairButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(adicionarButton)
                        .addGap(61, 61, 61)
                        .addComponent(imprimirButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 430, Short.MAX_VALUE)
                        .addComponent(sairButton))
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(adicionarButton)
                    .addComponent(imprimirButton)
                    .addComponent(sairButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void adicionarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_adicionarButtonActionPerformed
        CreateSinistroDialog dialog = new CreateSinistroDialog(null, true, StateEntityWindow.NEW, null);
        dialog.setVisible(true);
        Sinistro sinistro = dialog.objetoSinistro();
        if (sinistro != null) {
            tableModel.add(sinistro);
            int row = tableModel.indexOf(sinistro);
            table.setRowSelectionInterval(row, row);
            countRows();
        }
    }//GEN-LAST:event_adicionarButtonActionPerformed

    private void tableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableMouseClicked
        if (evt.getClickCount() == 2) {
            int row = table.getSelectedRow();
            if (row >= 0) {
                Sinistro sinistro = tableModel.getValue(row);
                if (sinistro != null) {
                    if (!isSelectEntity()) {
                        try {
                            //recupera as fotos do sinistro
                            SinistroService sinistroService = new SinistroService();
                            setCursor(new Cursor(Cursor.WAIT_CURSOR));
                            sinistroService.getFotos(sinistro);
                            setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                            UpdateSinistroDialog dialog = new UpdateSinistroDialog(null, true, StateEntityWindow.UPDATE, sinistro);
                            dialog.setVisible(true);
                            tableModel.fireTableDataChanged();
                            table.setRowSelectionInterval(row, row);
                        } catch (PersistenciaException pex) {
                            System.out.println(pex.getMessage());
                        }
                    } else {
                        setSinistro(sinistro);
                        dispose();
                    }
                }
            }
        }
    }//GEN-LAST:event_tableMouseClicked

    private void termoFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_termoFieldActionPerformed
        findSinistro(termoField.getText(), filtroCombo.getSelectedIndex());
    }//GEN-LAST:event_termoFieldActionPerformed

    private void pesquisarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pesquisarButtonActionPerformed
        findSinistro(termoField.getText(), filtroCombo.getSelectedIndex());
    }//GEN-LAST:event_pesquisarButtonActionPerformed

    private void filtroComboItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_filtroComboItemStateChanged
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            int index = filtroCombo.getSelectedIndex();
            if (index == 1) {
                setMask(PLACA_MASK, true);
            } else {
                setMask(null, false);
            }
        }
    }//GEN-LAST:event_filtroComboItemStateChanged

    private void excluirLinkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_excluirLinkActionPerformed
        int row = table.getSelectedRow();
        if (row >= 0) {
            Sinistro sinistro = tableModel.getValue(row);
            if (sinistro != null) {
                Object[] opcoes = {"Sim", "Não"};
                int resposta = JOptionPane.showOptionDialog(this,
                        "Confirma excluir o sinistro do veículo placa " + sinistro.getVeiculo().getPlaca() + " ?",
                        "Atenção", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
                        null, opcoes, opcoes[1]);
                if (resposta == 0) {
                    try {
                        SinistroService sinistroService = new SinistroService();
                        if (sinistroService.excluir(sinistro)) {
                            tableModel.remove(sinistro);
                            countRows();
                        }
                    } catch (PersistenciaException pex) {
                        System.out.println(pex.getMessage());
                    }
                }
            }
        }
    }//GEN-LAST:event_excluirLinkActionPerformed

    private void recentesLinkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_recentesLinkActionPerformed
        try {
            SinistroService sinistroService = new SinistroService();
            showResults(sinistroService.listarTodos(new int[]{0, 10}));
        } catch (PersistenciaException pex) {
            System.out.println(pex.getMessage());
        }
}//GEN-LAST:event_recentesLinkActionPerformed

    private void sairButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sairButtonActionPerformed
        dispose();
    }//GEN-LAST:event_sairButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            System.out.println(ex);
        }
        //</editor-fold>
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                ConsultaSinistroDialog dialog = new ConsultaSinistroDialog(new javax.swing.JFrame(), true, StateEntityWindow.SEARCH);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton adicionarButton;
    private javax.swing.JLabel countLabel;
    private org.jdesktop.swingx.JXHyperlink excluirLink;
    private javax.swing.JComboBox filtroCombo;
    private javax.swing.JButton imprimirButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton pesquisarButton;
    private org.jdesktop.swingx.JXHyperlink recentesLink;
    private javax.swing.JButton sairButton;
    private javax.swing.JTable table;
    private javax.swing.JFormattedTextField termoField;
    // End of variables declaration//GEN-END:variables

    private void organizeWindow() {
        try {
            tableModel = new ObjectTableModel<Sinistro>(Sinistro.class, "id,data,valorPrejuizo,associado,veiculo,placa");
            table.setModel(tableModel);
            filtroCombo.setSelectedItem(filters[0]);

            TableColumn col0 = table.getColumnModel().getColumn(0);
            TableColumn col1 = table.getColumnModel().getColumn(1);
            TableColumn col2 = table.getColumnModel().getColumn(2);
            TableColumn col3 = table.getColumnModel().getColumn(3);
            TableColumn col4 = table.getColumnModel().getColumn(4);
            TableColumn col5 = table.getColumnModel().getColumn(5);

            DefaultTableCellRenderer headerRenderer = new DefaultTableCellRenderer();
            headerRenderer.setHorizontalAlignment(SwingConstants.CENTER);

            DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer();
            cellRenderer.setHorizontalAlignment(SwingConstants.CENTER);

            col0.setHeaderRenderer(headerRenderer);
            col1.setHeaderRenderer(headerRenderer);
            col2.setHeaderRenderer(headerRenderer);
            col3.setHeaderRenderer(headerRenderer);
            col4.setHeaderRenderer(headerRenderer);
            col5.setHeaderRenderer(headerRenderer);

            col0.setCellRenderer(cellRenderer);
            col1.setCellRenderer(cellRenderer);
            col2.setCellRenderer(cellRenderer);
            col4.setCellRenderer(cellRenderer);
            col5.setCellRenderer(cellRenderer);

            termoField.requestFocus();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    private void showResults(List<Sinistro> sinistroList) {
        tableModel.setData(sinistroList);
        countRows();
    }

    private void countRows() {
        if (tableModel.isEmpty()) {
            countLabel.setText("Nenhum registro localizado");
        } else {
            int count = tableModel.getRowCount();
            if (count == 1) {
                countLabel.setText("1 registro localizado");
            } else {
                countLabel.setText(count + " registros localizados");
            }
        }
        fitColumns();
    }

    public StateEntityWindow getStateEntity() {
        return stateEntity;
    }

    private void findSinistro(String termo, int filterIndex) {
        try {
            termoField.requestFocus();
            termoField.selectAll();
            if (termo == null || termo.isEmpty()) {
                return;
            }
            SinistroService sinistroService = new SinistroService();
            String filter = filters[filterIndex];
            if (filters[0].equals(filter)) {
                showResults(sinistroService.listarPeloAssociado(termo));
            } else if (filters[1].equals(filter)) {
                showResults(sinistroService.listarPelaPlaca(termo));
            }
        } catch (PersistenciaException pex) {
            System.out.println(pex.getMessage());
        }
    }

    private void setMask(String mask, boolean show) {
        try {
            if (maskFormatter == null) {
                maskFormatter = new MaskFormatter(mask);
            } else {
                maskFormatter.setMask(mask);
            }
            if (show == true) {
                maskFormatter.install(termoField);
            } else {
                maskFormatter.uninstall();
            }
        } catch (Exception ex) {
        }
    }

    public boolean isSelectEntity() {
        return selectEntity;
    }

    public void setSelectEntity(boolean selectEntity) {
        this.selectEntity = selectEntity;
    }

    public Sinistro getSinistro() {
        return sinistro;
    }

    public void setSinistro(Sinistro sinistro) {
        this.sinistro = sinistro;
    }

    private void fitColumns() {
        if (tableModel.isEmpty()) {
            return;
        }
        Resizer.fitAllColumns(table);
    }

    @Override
    protected JRootPane createRootPane() {
        ActionListener closeListener = new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                sairButtonActionPerformed(e);
            }
        };
        KeyStroke stroke =
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        JRootPane myRootPane = new JRootPane();
        myRootPane.registerKeyboardAction(
                closeListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);

        return myRootPane;
    }
}
